/* Copyright 2008 Valentin Jacquemin

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Amazon.ECS.Model;
using Amazon.ECS;
using Amazon.ECS.Query;
using Alexandrie.helper;
using Alexandrie.Model;
using System.IO;
using System.Windows.Forms;
using WindowsLive.Writer.Api;

namespace Alexandrie.dal
{
    public class BookSearch:IBookSearch
    {

        #region IBookSearch Members

        public IList<Book> getBooks(string query)
        {
            String accessKeyId = Plugin.plugin.getOptions().GetString("awsKey", "");

            AmazonECS service = new AmazonECSQuery(accessKeyId, "", AmazonECSLocale.FR);
            ItemSearchRequest request = new ItemSearchRequest();
            List<string> l = new List<string>();
            l.Add("Images");
            l.Add("Request");
            l.Add("ItemAttributes");
            request.ResponseGroup = l;
            request.Keywords = query;
            request.SearchIndex = "Books";

            try
            {
                Book b;
                ItemSearchResponse response = service.ItemSearch(request);
                List<Items> itemsList = response.Items;
                IList<Book> ret = new List<Book>();
                foreach (Items its in itemsList)
                {
                    List<Item> it = its.Item;
                    foreach (Item item in it)
                    {
                        String author = "no name";
                        if (item.ItemAttributes.IsSetAuthor() && item.ItemAttributes.Author.Count > 0)
                        {
                            author = item.ItemAttributes.Author[0];
                        }
                        String title = "no title";
                        if (item.ItemAttributes.IsSetTitle())
                        {
                            title = item.ItemAttributes.Title;
                        }
                        String img = "";
                        if (item.IsSetMediumImage() && item.MediumImage.IsSetURL())
                        {
                            img = item.MediumImage.URL.ToString();
                        }
                        b = new Book();
                        b.author = author;
                        b.titleBook = title;
                        if (img.Length > 0)
                        {
                            PluginHttpRequest stream = new PluginHttpRequest(img);
                            b.image = System.Drawing.Image.FromStream(stream.GetResponse(), true);
                        }
                        ret.Add(b);
                    }

                }
                return ret;
            }
            catch (AmazonECSException ex)
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Request ID: " + ex.RequestId);
                Console.WriteLine("XML: " + ex.XML);
            }
            return null;
        }

        #endregion
    }
}
